package ua.org.nuos.sdms.middle.entity;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 29.03.12
 * Time: 20:43
 * To change this template use File | Settings | File Templates.
 */
@Entity
@Table(name = "teacher")
@NamedQueries(value = {
        @NamedQuery(name = "findTeacherByName", query = "SELECT DISTINCT t FROM Teacher t WHERE t.name = :name AND t.group.id = :group_id ORDER BY t.name"),
        @NamedQuery(name = "findTeacherBySubject", query = "SELECT DISTINCT t FROM Teacher t, Subject s WHERE s.id = :subject_id AND t MEMBER OF s.teachers AND t.group.id = :group_id ORDER BY t.name"),
        @NamedQuery(name = "findTeacherByGroup", query = "SELECT DISTINCT t FROM Teacher t WHERE t.group.id = :group_id ORDER BY t.name")
})
public class Teacher {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long id;

    @Column(name = "name")
    private String name;

    @Lob
    @Column(name = "about")
    private String about;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "group_id", nullable = false)
    private Group group;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Teacher teacher = (Teacher) o;

        if (id != teacher.id) return false;
        if (about != null ? !about.equals(teacher.about) : teacher.about != null) return false;
        if (name != null ? !name.equals(teacher.name) : teacher.name != null) return false;
        if (group != null ? !group.equals(teacher.group) : teacher.group != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (id ^ (id >>> 32));
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (about != null ? about.hashCode() : 0);
        result = 31 * result + (group != null ? group.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", about='" + about + '\'' +
                ", group='" + group +
                '}';
    }
}
